package com.hzb.LNReader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.Serializable;

import android.util.Log;

public class ReaderContext implements Serializable {
	private static final long serialVersionUID = -3615126625347154286L;
	private static File configFile;
	
	private static final String LOG = "ReaderContext";
	
	public static int lineBufferSize = 40000;
	public static int readLineLimit = 10000;
	public static int backSeekSize = 256 * 1024;
	
	public static int viewBufferHeight = 8000;
	public static int fillLineCount = 60;
	
	public static int fontSize = 30;
	public static int fontColor = 0xFF808080;
	
	public static int startFillLine = 40;
	
	public static String rootFolder = "/sdcard/ebook/";
	
	public static ReadHistory head;
	
	
	public static void reset() {
		rootFolder = "/sdcard/ebook/";
		
		lineBufferSize = 40000;
		readLineLimit = 10000;
		backSeekSize = 256 * 1024;
		startFillLine = 40;
		
		viewBufferHeight = 8000;
		fillLineCount = 60;
		
		fontSize = 30;
		fontColor = 0xFF808080;
	}
	
	public static void loadFrom(File file) {
		configFile = file;
		if ((!file.exists()) || (!file.isFile())) return;
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			try {
				rootFolder = in.readLine();
				
				lineBufferSize = Integer.parseInt(in.readLine());
				readLineLimit = Integer.parseInt(in.readLine());
				backSeekSize = Integer.parseInt(in.readLine());
				startFillLine = Integer.parseInt(in.readLine());
				
				viewBufferHeight = Integer.parseInt(in.readLine());
				fillLineCount = Integer.parseInt(in.readLine());
				
				fontSize = Integer.parseInt(in.readLine());
				fontColor = Integer.parseInt(in.readLine());
				
				if (!rootFolder.endsWith("/")) {
					rootFolder += '/';
				}
				
				
				ReadHistory last = head = new ReadHistory();
				
				for (;;) {
					String filePath = in.readLine();
					if (filePath == null) break;
					
					ReadHistory record = new ReadHistory();
					record.file = new File(filePath);
					record.scrollOffset = Integer.parseInt(in.readLine());
					record.fileOffset = Integer.parseInt(in.readLine());
					record.startLineSeq = Integer.parseInt(in.readLine());
					record.encoding = in.readLine();
					record.lastReadTime = Long.parseLong(in.readLine());
					record.lastModify = Long.parseLong(in.readLine());
					record.maxLineSeq = Integer.parseInt(in.readLine());
					record.summary = in.readLine();
					record.process = Integer.parseInt(in.readLine());
					
					if (!record.file.isFile()) continue;
					if (record.file.lastModified() !=  record.lastModify) {
						record.reset();
					}
					record.fileSize = (int) record.file.length();
					record.prev = last;
					last.next = record;
					last = record;
				}
				
				head = head.next;
				head.prev = null;
			} finally {
				in.close();
			}
		} catch (Exception e) {
			Log.e(LOG, "Error loading config", e);
			reset();
			head = null;
		}
	}
	
	public static void save() {
		try {
			PrintWriter out = new PrintWriter(configFile);
			try {
				out.println(rootFolder);
				
				out.println(lineBufferSize);
				out.println(readLineLimit);
				out.println(backSeekSize);
				out.println(startFillLine);
				
				out.println(viewBufferHeight);
				out.println(fillLineCount);
				
				out.println(fontSize);
				out.println(fontColor);

				ReadHistory iter = head;
				for (;iter != null; iter = iter.next) {
					if (iter.file.isFile()) {
						out.println(iter.file.getAbsolutePath());
						out.println(iter.scrollOffset);
						out.println(iter.fileOffset);
						out.println(iter.startLineSeq);
						out.println(iter.encoding);
						out.println(iter.lastReadTime);
						out.println(iter.lastModify);
						out.println(iter.maxLineSeq);
						out.println(iter.summary);
						out.println(iter.process);
					} else {
						iter.remove();
					}
				}
			} finally {
				out.close();
			}
		} catch (Exception e) {
			Log.e(LOG, "Error saving config", e);
		}
	}
	
	public static ReadHistory openReadHistory(File file) {
		ReadHistory iter = head;
		for (;iter != null; iter = iter.next) {
			if (iter.file.equals(file)) {
				iter.offer();
				return iter;
			}
			
		}
		return new ReadHistory(file);
	}
	
}


